import urllib
import urllib.request
import re

def download_page(url):
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    data = response.read()
    return data

def get_img_from_html(html):
    regx = r'http://[\S]*\.jpg'
    pattern = re.compile(regx)
    get_img = re.findall(pattern, repr(html))   #repr用来转换表达式类型字符串
    num = 1
    for img in get_img:
        image = download_page(img)  #将每个img链接重新解析
        with open('%s.jpg'%num, 'wb') as fp:
            fp.write(image)
            num += 1
            print('正在下载第%s张图片'%num)
    return

def get_image(url):
    request = urllib.request.Request(url)   #构建请求
    response = urllib.request.urlopen(request)  #获取服务器响应
    get_img = response.read()
    with open('001.jpg', 'wb') as fp:
        fp.write(get_img)
        print('图片下载完成')
    return

url = 'http://p2.123.sogoucdn.com/imgu/2016/10/20161019124600_428.jpg'
# get_image(url)
durl = 'http://pic.yesky.com/451/106166451.shtml'
html = download_page(durl)
get_img_from_html(html)